<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('凭证日期')" prop="voucherDate">
            <el-date-picker v-model="form.voucherDate" value-format="yyyy-MM-dd" type="date" :placeholder="$t('凭证日期')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('凭证字')" prop="voucherWordId">
            <el-select v-model="form.voucherWordId">
              <el-option
                v-for="item in finVoucherWordList"
                :key="item.id"
                :label="item.voucherWord"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('凭证摘要')" prop="voucherAbstract">
            <el-input v-model="form.voucherAbstract" :placeholder="$t('凭证摘要')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col v-if="configType === 'month'" :span="12">
          <el-form-item :label="$t('凭证分类')" prop="voucherClassify">
            <el-radio-group v-model="form.voucherClassify">
              <el-radio
                v-for="item in voucherClassifyOptions"
                :key="item.dictValue"
                :label="item.dictValue"
              > {{ $t(item.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="configType === 'month' ? $t('“以前年度损益调整”科目') : $t('“年度利润调整”科目')" prop="subjectId">
            <el-select v-model="form.subjectId" filterable clearable>
              <el-option
                v-for="ele in fundAccountSubjectList"
                :key="ele.subjectCode"
                :label="ele.subjectCode + ' ' +ele.subjectName"
                :value="ele.subjectCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="configType === 'month' ? $t('“以前年度损益调整”科目的结转科目') : $t('“年度利润调整”科目的结转科目')" prop="targetSubjectId">
            <el-select v-model="form.targetSubjectId" filterable clearable>
              <el-option
                v-for="ele in accountSubjectCarryoverList"
                :key="ele.subjectCode"
                :label="ele.subjectCode + ' ' +ele.subjectName"
                :value="ele.subjectCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="configType === 'month'" :span="12">
          <el-form-item :label="$t('其他损益科目的结转科目')" prop="otherSubjectId">
            <el-select v-model="form.otherSubjectId" filterable clearable>
              <el-option
                v-for="ele in accountSubjectCarryoverList"
                :key="ele.subjectCode"
                :label="ele.subjectCode + ' ' +ele.subjectName"
                :value="ele.subjectCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="fin:carryoverRecord" />
    </div>
  </div>
</template>

<script>
    import { listFinVoucherWord } from '@/api/fin/voucherWord/finVoucherWord'
    import { getFundAccountSubjectList } from '@/api/fin/fundAccount/finFundAccountDetail'
    import { getByFundAccountIdAndCarryoverRecordId, saveFinCarryoverVoucherConfig } from '@/api/fin/carryoverRecord/finCarryoverVoucherConfig'
    export default {
        props: {
          configType: {
            type: String,
            require: true,
            default: 'month' // 期末：month, 年末：year
          }
        },
        data() {
            return {
                // 表单参数
                form: {},
                finVoucherWordList: [], // 凭证字列表
                // 遮罩层
                loading: false,
                fundAccountSubjectList: [],
                // 凭证分类字典
                voucherClassifyOptions: [],
                // 科目
                accountSubjectCarryoverList: [],
                // 表单校验
                rules: {
                  voucherDate: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  voucherWordId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  voucherAbstract: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  otherSubjectId: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  voucherClassify: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                }
            }
        },
        created() {
          this.getDicts('voucher_classify').then(response => {
            this.voucherClassifyOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
              this.loading = true
              this.getVoucherWordList()
              if (data != null) {
                this.reset()
                getByFundAccountIdAndCarryoverRecordId(data.fundAccountId, data.carryoverRecordId).then(response => {
                  if (response.data != null) {
                    console.log('11111111111111111111111', this.configType)
                    this.form = response.data
                    if (this.configType === 'month') {
                      this.getSubjectList(this.form.fundAccountId)
                    } else if (this.configType === 'year') {
                      this.getSubjectYearList(this.form.fundAccountId)
                    }
                    this.loading = false
                  } else {
                    console.log('22222222222222222', this.configType)
                    this.form.fundAccountId = data.fundAccountId
                    this.form.carryoverRecordId = data.carryoverRecordId
                    if (this.configType === 'month') {
                      this.form.voucherDate = this.getLastDayOfMonth(data.accountYearMonth.replace('年', '-').replace('月', '') + '-01')
                      this.getSubjectList(this.form.fundAccountId)
                    } else if (this.configType === 'year') {
                      this.form.voucherDate = data.accountYearMonth + '-12-31'
                      this.getSubjectYearList(this.form.fundAccountId)
                    }
                    this.loading = false
                  }
                })
              } else {
                this.msgError(this.$t('请先维护基金账套！'))
                this.loading = false
                this.$parent.$parent.configOpen = false
              }
            },
            // 根据字符串获取当期月份的最后一天日期
            getLastDayOfMonth(dateString) {
              const date = new Date(dateString)
              return new Date(date.getFullYear(), date.getMonth() + 1, 0)
            },
            // 根据基金账套
            getSubjectList(fundAccountId) {
              if (fundAccountId === null || fundAccountId === undefined || fundAccountId === '') {
                return
              }
              const paramData = {
                fundAccountId: fundAccountId,
                subjectType: 'SY'
              }
              getFundAccountSubjectList(paramData).then(response => {
                this.fundAccountSubjectList = response.data
              })

              const param = {
                fundAccountId: fundAccountId,
                subjectType: 'QY'
              }
              getFundAccountSubjectList(param).then(response => {
                this.accountSubjectCarryoverList = response.data
              })
            },
            // 根据基金账套(年)
            getSubjectYearList(fundAccountId) {
              if (fundAccountId === null || fundAccountId === undefined || fundAccountId === '') {
                return
              }
              const param = {
                fundAccountId: fundAccountId,
                subjectType: 'QY'
              }
              getFundAccountSubjectList(param).then(response => {
                this.fundAccountSubjectList = response.data
                this.accountSubjectCarryoverList = response.data
                this.form.subjectId = '4103'
                this.form.targetSubjectId = '410406'
              })
            },
            // 获取凭证字列表
            getVoucherWordList() {
              this.finVoucherWordList = []
              listFinVoucherWord({}).then(response => {
                this.finVoucherWordList = response.rows
                // 初始化把凭证字列表中的默认赋值
                if (this.finVoucherWordList && this.finVoucherWordList.length > 0) {
                  this.finVoucherWordList.forEach(item => {
                    if (item.whetherDefault === 'Y') {
                      this.form.voucherWordId = item.id
                    }
                  })
                }
              })
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    fundAccountId: undefined,
                    carryoverRecordId: undefined,
                    voucherDate: undefined,
                    voucherWordId: undefined,
                    voucherAbstract: this.configType === 'month' ? '结转本期损益' : '结转年末利润',
                    voucherClassify: this.configType === 'month' ? 'FKJZ' : '',
                    subjectId: undefined,
                    targetSubjectId: undefined,
                    otherSubjectId: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.configOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.configOpen !== undefined) {
                  this.$parent.$parent.configOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveFinCarryoverVoucherConfig(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
